package com.www.dao.admin;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.www.domain.admin.User;

/**
 * user用户dao
 */
@Repository
public interface UserDao {
	/*
	 * 根据用户名查找用户
	 */
	@Select("select * from user where username = #{username}")
	public User findByUsername(String username);
	
	/*
	 * 添加用户
	 */
	@Insert("insert into user(id,username,password,roleId,photo,sex,age,address) values(default,#{username},#{password},#{roleId},#{photo},#{sex},#{age},#{address})")
	public int add(User user);
	
	/*
	 * 修改用户
	 */
	@Update("update user set username = #{username},roleId = #{roleId},photo = #{photo},sex = #{sex},age = #{age},address = #{address} where id = #{id}")
	public int edit(User user);
	
	/*
	 * 修改用户密码
	 */
	@Update("update user set password = #{password} where id = #{id}")
	public int editPassword(User user);
	
	/*
	 * 删除用户
	 */
	@Delete("delete from user where id in(${value})")
	public int delete(String ids);
	
	/*
	 * 分页获取用户列表 
	 */
	@Select({"<script>" +
			"select * from user " +
			"where  1=1 " +
			"<if test='username != null'> and username like '%${username}%'</if>" +
			"<if test='roleId != null'> and roleId like '%${roleId}%'</if>" +
			"<if test='sex != null'> and sex like '%${sex}%'</if>" +
			"<if test='offset != null and pageSize != null'> limit #{offset},#{pageSize}</if>" +
		"</script>"})
	public List<User> findList(Map<String, Object> queryMap);
	
	/*
	 * 获取符合结果的总记录数
	 */
	@Select({"<script>" +
			"select count(*) from user " +
			"where  1=1 " +
			"<if test='username != null'> and username like '%${username}%'</if>" +
			"<if test='roleId != null'> and roleId like '%${roleId}%'</if>" +
			"<if test='sex != null'> and sex like '%${sex}%'</if>" +
		"</script>"})
	public int getTotal(Map<String, Object> queryMap);
}
